package com.intexh.quickandroid.module.chat.bean;


import com.intexh.quickandroid.module.chat.sprinkles.ManyQuery;
import com.intexh.quickandroid.module.chat.sprinkles.Model;
import com.intexh.quickandroid.module.chat.sprinkles.OneQuery;
import com.intexh.quickandroid.module.chat.sprinkles.Query;
import com.intexh.quickandroid.module.chat.sprinkles.QueryBuilder;
import com.intexh.quickandroid.module.chat.sprinkles.annotations.Column;
import com.intexh.quickandroid.module.chat.sprinkles.annotations.PrimaryKey;
import com.intexh.quickandroid.module.chat.sprinkles.annotations.Table;
import com.intexh.quickandroid.utils.GsonUtils;

import java.util.ArrayList;
import java.util.List;


@Table("ChatMessage")
public class ChatMessage extends Model {
   
	//增加字段时,须在EasemobManager的beanFromChatMessage()中增加对应代码
    public static final int PROGRESS_COMPLETE = 1;  //发送成功
    public static final int PROGRESS_FAIL = -1;     //发送失败
    public static final int PROGRESS_NO_SEND = 0;   //未发送
    public static final int PROGRESS_SENDING = -2;   //发送中

    public static final int TYPE_TEXT = 1;
    public static final int TYPE_IMAGE = 2;
    public static final int TYPE_BLEND=3;//图文混合(商品)
    public static final int TYPE_FACE = 6;
    public static final int TYPE_VOICE = 7;
    public static final int TYPE_LOCATION = 8; //位置共享
    @PrimaryKey
    @Column("id")
    private String id;
    @Column("content")
    private String content;
    @Column("type")
    private Integer type;	//文本1 图片2 图文混和 3  表情6 语音7 定位消息8
    @Column("create_time")
    private long create_time;
    @Column("easemob_id")	//环信的个人id或群id
    private String easemob_id;
    @Column("uid")
    private String uid;	//
    @Column("class_id")
    private String class_id;	//
    @Column("localUrl")
    private String localUrl;
    @Column("progress")
    private int progress;
    @Column("played")
    private int played;
    @Column("voiceLength")
    private int voiceLength;  //环信3.0后为int型

    @Column("sex")
    private int sex;
    @Column("is_anonymous")
    private int is_anonymous;
    @Column("anonymous_name")
    private String anonymous_name;

    @Column("is_group")
    private boolean is_group;

    @Column("emConnversationName")
    private String emConnversationName;

    @Column("group_id")			//群id
    private String group_id;
    @Column("group_name")			//群名称
    private String group_name;
    @Column("group_avatar")			//群名称
    private String group_avatar;    //群头像

    @Column("display_name")
    private String display_name;	//发送人昵称

    @Column("avatar")
    private String avatar;	//发送人头像

    @Column("longitude")
    private double longitude;
    @Column("latitude")
    private double latitude;

    @Column("at_uid")
    private String at_uid;


    @Column("prop_id")
    private String prop_id;   //道具的id
    @Column("prop_url")
    private String prop_url;   //道具的图片地址

    @Column("dynamic_id")
    private String dynamic_id;

    @Column("source_type")
    private int source_type;  //1是普通群聊,2是发送求助时的群聊

    @Column("coin_change")
    private int coin_change;

    @Column("icon_url")
    private String icon_url;

    @Column("blend_cover")
    private String blend_cover;//混合消息的图片

    @Column("blend_name")
    private String blend_name;//混合消息的名字

    @Column("blend_location")
    private String blend_location;//混合消息的地址

    @Column("blend_id")
    private long blend_id;

    @Column("blend_type")
    private int blend_type;

    public String getGroup_avatar() {
        return group_avatar;
    }

    public void setGroup_avatar(String group_avatar) {
        this.group_avatar = group_avatar;
    }

    public void setClass_id(String class_id) {
        this.class_id = class_id;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public long getCreate_time() {
        return create_time;
    }

    public void setCreate_time(long create_time) {
        this.create_time = create_time;
    }

    public String getEasemob_id() {
        return easemob_id;
    }

    public void setEasemob_id(String easemob_id) {
        this.easemob_id = easemob_id;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getLocalUrl() {
        return localUrl;
    }

    public void setLocalUrl(String localUrl) {
        this.localUrl = localUrl;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public int getPlayed() {
        return played;
    }

    public void setPlayed(int played) {
        this.played = played;
    }

    public int getVoiceLength() {
        return voiceLength;
    }

    public void setVoiceLength(int voiceLength) {
        this.voiceLength = voiceLength;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public int getIs_anonymous() {
        return is_anonymous;
    }

    public void setIs_anonymous(int is_anonymous) {
        this.is_anonymous = is_anonymous;
    }

    public String getAnonymous_name() {
        return anonymous_name;
    }

    public void setAnonymous_name(String anonymous_name) {
        this.anonymous_name = anonymous_name;
    }

    public boolean is_group() {
        return is_group;
    }

    public void setIs_group(boolean is_group) {
        this.is_group = is_group;
    }

    public String getEmConnversationName() {
        return emConnversationName;
    }

    public void setEmConnversationName(String emConnversationName) {
        this.emConnversationName = emConnversationName;
    }

    public String getGroup_name() {
        return group_name;
    }

    public void setGroup_name(String group_name) {
        this.group_name = group_name;
    }

    public String getDisplay_name() {
        return display_name;
    }

    public void setDisplay_name(String display_name) {
        this.display_name = display_name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public String getAt_uid() {
        return at_uid;
    }

    public void setAt_uid(String at_uid) {
        this.at_uid = at_uid;
    }

    public String getGroup_id() {
        return group_id;
    }

    public void setGroup_id(String group_id) {
        this.group_id = group_id;
    }

    public String getProp_id() {
        return prop_id;
    }

    public void setProp_id(String prop_id) {
        this.prop_id = prop_id;
    }

    public String getProp_url() {
        return prop_url;
    }

    public void setProp_url(String prop_url) {
        this.prop_url = prop_url;
    }

    public String getDynamic_id() {
        return dynamic_id;
    }

    public void setDynamic_id(String dynamic_id) {
        this.dynamic_id = dynamic_id;
    }

    public int getSource_type() {
        return source_type;
    }

    public void setSource_type(int source_type) {
        this.source_type = source_type;
    }

    public int getCoin_change() {
        return coin_change;
    }

    public void setCoin_change(int coin_change) {
        this.coin_change = coin_change;
    }

    public String getIcon_url() {
        return icon_url;
    }

    public void setIcon_url(String icon_url) {
        this.icon_url = icon_url;
    }

    public String getBlend_cover() {
        return blend_cover;
    }

    public void setBlend_cover(String blend_cover) {
        this.blend_cover = blend_cover;
    }

    public String getBlend_name() {
        return blend_name;
    }

    public void setBlend_name(String blend_name) {
        this.blend_name = blend_name;
    }

    public String getBlend_location() {
        return blend_location;
    }

    public void setBlend_location(String blend_location) {
        this.blend_location = blend_location;
    }

    public long getBlend_id() {
        return blend_id;
    }

    public void setBlend_id(long blend_id) {
        this.blend_id = blend_id;
    }

    public int getBlend_type() {
        return blend_type;
    }

    public void setBlend_type(int blend_type) {
        this.blend_type = blend_type;
    }

    public ChatMessage() {

    }

    public ChatMessage(String id) {
        this.id = id;
    }

    public static ChatMessage get(String id) {
        OneQuery<ChatMessage> oneQuery = Query.one(ChatMessage.class,  "select * from ChatMessage where id = ?", id);
        if (oneQuery != null)
            return oneQuery.get();
        else
            return null;
    }

    public static List<ChatMessage> getAll(String sessionId) {
        ManyQuery<ChatMessage> manyQuery = Query.many(ChatMessage.class, "select * from ChatMessage where easemob_id=? order by create_time ", sessionId);
        if (manyQuery == null) return new ArrayList<>();
        return manyQuery.get().asList();
    }

    public static List<ChatMessage> getAll() {
        ManyQuery<ChatMessage> manyQuery = Query.many(ChatMessage.class, "select * from ChatMessage");
        if (manyQuery == null) return new ArrayList<>();
        return manyQuery.get().asList();
    }

    public static void deleteAllBySessionId(String sessionId) {
        List<ChatMessage> list = ChatMessage.getAll(sessionId);
        for (ChatMessage m : list) {
            m.deleteAsync();
        }
    }

    public static ChatMessage getLatestBySessionId(String sessionId) {
        OneQuery<ChatMessage> oneQuery = Query.one(ChatMessage.class,
                "select * from ChatMessage where sessionId=? and (type=1 or type=2 or type=6 or type=7) order by create_time desc", sessionId);
        if (oneQuery != null)
            return oneQuery.get();
        else
            return null;
    }



	public static boolean isRepetitiveMessage(long uid, long time) {

        OneQuery<ChatMessage> oneQuery = Query.one(ChatMessage.class, "select * from ChatMessage where uid=? and create_time=?", uid, time);
        ChatMessage chatMessage = null;
        if (oneQuery != null) {
            chatMessage = oneQuery.get();
        }
		return chatMessage != null;
	}

    public static void deleteAll(){
        List<ChatMessage> list = ChatMessage.getAll();
        for(ChatMessage m:list){
            m.deleteAsync();
        }
    }

    @Override
    public String toString() {
        return GsonUtils.serializedToJson(this);
    }

    public static List<ChatMessage> getPagedMessage(String sessionId, long time, int pageSize) {
        QueryBuilder<ChatMessage> queryBuilder = QueryBuilder.create(ChatMessage.class)
                .order("create_time desc")
                .where("easemob_id='" + sessionId + "'")
                .limit(pageSize);
        if(time > 0){
            queryBuilder.where("create_time<" + time);
        }
        if(queryBuilder == null) return new ArrayList<>();
        return queryBuilder.list();
    }
}
